package pagerbanner.library.com;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.List;

/**
 * author:吴昶
 * 时间: ${data}
 * 简介：
 */
public class BannerAdapter<T> extends PagerAdapter {

    private OnBannerListener<T> onBannerListener;

    private List<T> data;

    private Context context;

    BannerAdapter(Context context, OnBannerListener<T> onBannerListener){
        this.context=context;
        this.onBannerListener=onBannerListener;
    }

    void upDataItems(List<T> data){
        this.data=data;
        notifyDataSetChanged();
    }

    @Override
    public int getCount() {
        return data==null?0:data.size();
    }

    @Override
    @NonNull
    public Object instantiateItem(@NonNull ViewGroup container, final int position) {
        View view=onBannerListener.onBannerView(context,data.get(position),position);
        view.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                onBannerListener.onBannerItemClick(data.get(position),position);
            }
        });
        container.addView(view,0);
        return view;
    }

    @Override
    public void destroyItem(@NonNull ViewGroup container, int position, @NonNull Object object) {
        container.removeView((View) object);
    }

    @Override
    public boolean isViewFromObject(@NonNull View view, @NonNull Object object) {
        return object==view;
    }
}
